package model.security;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import core.security.MyUserDetail;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

public class UsrUser  implements MyUserDetail,java.io.Serializable {
     private Long userid;
     private String useraccount;
     private String email;
     private String userpassword;
     private String userdesc;
     private Boolean enabled;
     private Boolean isadmin;
     private Timestamp createtime;
     private String realname;
     private String nickname;
     private String phone;
     private String fax;
     private String address;
     private String zip;
     private String photo;
     private Set usrUserRoles = new HashSet(0);

   //实现了UserDetails之后的相关变量
     private  String myusername;
     private  String mypassword;
     private  boolean accountNonExpired;
     private  boolean accountNonLocked;
     private  boolean credentialsNonExpired;
     private  Set<GrantedAuthority> authorities;

    // Constructors

    /** default constructor */
    public UsrUser() {
    }

    
    /** full constructor */
    public UsrUser(String useraccount, String userpassword, String userdesc, Boolean enabled, Boolean isadmin, Timestamp createtime, 
    	     String realname,String nickname,String phone,String fax,String address,String zip,String photo,Set usrUserRoles,
    		boolean accountNonExpired,boolean credentialsNonExpired, boolean accountNonLocked, 
    		Collection<GrantedAuthority> authorities) {
    	
        if (((useraccount == null) || "".equals(useraccount)) || (userpassword == null)) {
            throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
        }
    	
        this.useraccount = useraccount;
        this.userpassword = userpassword;
        this.userdesc = userdesc;
        this.enabled = enabled;
        this.isadmin = isadmin;
        this.createtime = createtime;
        this.realname = realname;
        this.nickname = nickname;
        this.phone = phone;
        this.fax = fax;
        this.address = address;
        this.zip = zip;
        this.photo = photo;
        this.usrUserRoles = usrUserRoles;
        
        //------------------------
        this.myusername=useraccount;
        this.mypassword=userpassword;
        this.accountNonExpired = accountNonExpired;
        this.credentialsNonExpired = credentialsNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
    }

   
    // Property accessors

    public Long getUserid() {
        return this.userid;
    }
    
    public void setUserid(Long userid) {
        this.userid = userid;
    }

    public String getUseraccount() {
        return this.useraccount;
    }
    
    public void setUseraccount(String useraccount) {
        this.useraccount = useraccount;
    }

    public String getUserpassword() {
        return this.userpassword;
    }
    
    public void setUserpassword(String userpassword) {
        this.userpassword = userpassword;
    }

    public String getUserdesc() {
        return this.userdesc;
    }
    
    public void setUserdesc(String userdesc) {
        this.userdesc = userdesc;
    }

    public Boolean getEnabled() {
        return this.enabled;
    }
    
    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Boolean getIsadmin() {
        return this.isadmin;
    }
    
    public void setIsadmin(Boolean isadmin) {
        this.isadmin = isadmin;
    }

    public Timestamp getCreatetime() {
        return this.createtime;
    }
    
    public void setCreatetime(Timestamp createtime) {
        this.createtime = createtime;
    }
    
    public String getRealname() {
		return realname;
	}


	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getNickname() {
		return nickname;
	}


	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPhone() {
		return phone;
	}


	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}
    public Set getUsrUserRoles() {
        return this.usrUserRoles;
    }
    
    public void setUsrUserRoles(Set usrUserRoles) {
        this.usrUserRoles = usrUserRoles;
    }
   
//++++++++++++++++++++
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }
    
    public void setAuthorities( Collection<GrantedAuthority> authorities ){
    	this.authorities = (Set<GrantedAuthority>) authorities;
    }
    
    public String getPassword() {
        return mypassword;
    }
    
    public String getUsername() {
        return myusername;
    }
    
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    public boolean isEnabled() {
        return enabled;
    }

    private static SortedSet<GrantedAuthority> sortAuthorities(Collection<GrantedAuthority> authorities) {
        Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
        // Ensure array iteration order is predictable (as per UserDetails.getAuthorities() contract and SEC-717)
        SortedSet<GrantedAuthority> sortedAuthorities =
            new TreeSet<GrantedAuthority>(new AuthorityComparator());

        for (GrantedAuthority grantedAuthority : authorities) {
            Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements");
            sortedAuthorities.add(grantedAuthority);
        }

        return sortedAuthorities;
    }

	public String getEmail() {
		return email;
	}


	public void setEmail(String email) {
		this.email = email;
	}

	private static class AuthorityComparator implements Comparator<GrantedAuthority>, Serializable {
        public int compare(GrantedAuthority g1, GrantedAuthority g2) {
            // Neither should ever be null as each entry is checked before adding it to the set.
            // If the authority is null, it is a custom authority and should precede others.
            if (g2.getAuthority() == null) {
                return -1;
            }

            if (g1.getAuthority() == null) {
                return 1;
            }

            return g1.getAuthority().compareTo(g2.getAuthority());
        }
    }


}
